package com.ruoyi.business.service.product.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.product.TbProductCategories;
import com.ruoyi.business.mapper.product.TbProductCategoriesMapper;
import com.ruoyi.business.service.product.TbProductCategoriesService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 商品分类表Service实现类
 */
@Service
@RequiredArgsConstructor
public class TbProductCategoriesServiceImpl extends ServiceImpl<TbProductCategoriesMapper, TbProductCategories>
        implements TbProductCategoriesService {
    private final TbProductCategoriesMapper baseMapper;
    @Override
    public List<TbProductCategories> selectCategoriesList(TbProductCategories categories) {
        return this.list();
    }

    @Override
    public TbProductCategories selectCategoriesById(Long id) {
        return this.getById(id);
    }

    @Override
    public boolean insertCategories(TbProductCategories categories) {
        return this.save(categories);
    }

    @Override
    public boolean updateCategories(TbProductCategories categories) {
        return this.updateById(categories);
    }

    @Override
    public boolean deleteCategoriesById(Long id) {
        return this.removeById(id);
    }

    @Override
    public boolean deleteCategoriesByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

    @Override
    public TableDataInfo<TbProductCategories> selectPageList(TbProductCategories tbProductCategories, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.selectPage(pageQuery.build(), this.getQueryWrapper(tbProductCategories)));
    }

    @Override
    public List<TbProductCategories> selectProductCategorieList(TbProductCategories tbProductCategories) {
        LambdaQueryWrapper<TbProductCategories> queryWrapper = getQueryWrapper(tbProductCategories);
        List<TbProductCategories> tbProductCategoriesList = baseMapper.selectList(queryWrapper);
        return tbProductCategoriesList;
    }

    private LambdaQueryWrapper<TbProductCategories> getQueryWrapper(TbProductCategories tbProductCategories) {
        return Wrappers.lambdaQuery(TbProductCategories.class)
            .like(StringUtils.isNotBlank(tbProductCategories.getName()),TbProductCategories::getName, tbProductCategories.getName())
            .eq(tbProductCategories.getLevel()!=null,TbProductCategories::getLevel, tbProductCategories.getLevel())
            .eq(tbProductCategories.getParentId()!=null,TbProductCategories::getParentId, tbProductCategories.getParentId())
            .eq(tbProductCategories.getIsVisible()!=null,TbProductCategories::getIsVisible,tbProductCategories.getIsVisible())
            .orderByAsc(TbProductCategories::getSortOrder);

    }

}
